package net.engio.mbassy.camel;

import java.util.Map;

import net.engio.mbassy.MBassador;

import org.apache.camel.Endpoint;
import org.apache.camel.impl.DefaultComponent;
import org.apache.camel.util.CamelContextHelper;

/**
 * Represents the component that manages {@link MbassadorEndpoint}.
 */
public class MbassadorComponent extends DefaultComponent {

    /**
     * MBassador event bus object instance.
     */
    private MBassador<?> eventBus;

    @Override
    protected Endpoint createEndpoint(final String uri, final String remaining, final Map<String, Object> parameters)
            throws Exception {
        MBassador<?> resolvedEventBus = eventBus;
        if (resolvedEventBus == null) {
            resolvedEventBus = CamelContextHelper.mandatoryLookup(getCamelContext(), remaining, MBassador.class);
        }

        final Endpoint endpoint = new MbassadorEndpoint(uri, this, resolvedEventBus);
        setProperties(endpoint, parameters);
        return endpoint;
    }

    /**
     * Returns {@link MBassador} event bus object.
     * @return {@link MBassador} event bus object
     */
    public MBassador<?> getEventBus() {
        return eventBus;
    }

    /**
     * Sets {@link MBassador} event bus object.
     * @param eventBus the {@link MBassador} event bus object to set
     */
    public void setEventBus(final MBassador<?> eventBus) {
        this.eventBus = eventBus;
    }
}
